package com.nttuan.service2;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;

import com.nttuan.model.dao.TaskDao;
import com.nttuan.model.dao.UserDao;
import com.nttuan.model.dao.service.TaskService;
import com.nttuan.model.dao.service.UserService;
import com.nttuan.model.entity.Task;
import com.nttuan.model.entity.User;
import com.nttuan.mycalender.R;
import com.nttuan.util.EmailParser;
import com.nttuan.util.GMail;
import com.nttuan.util.LogSupport;
import com.nttuan.util.Network;
import com.nttuan.util.Support;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by TuanNT on 12/12/2014.
 */
public class SMSReceive extends BroadcastReceiver{
    public final String TAG ="HelpReceive";
    //final SmsManager sms = SmsManager.getDefault();
    @Override
    public void onReceive(Context context, Intent intent) {
        if(!Network.isConnect(context))
            Network.setMobileDataEnabled(context, true);
        final Bundle bundle = intent.getExtras();
        try{
            if(bundle !=null){
                final Object[] pdusObj = (Object[]) bundle.get("pdus");
                String message;
                String[] arr;
                for (int i = 0; i < pdusObj.length; i++) {
                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    message = currentMessage.getDisplayMessageBody();
                    arr = message.split(",");
                    if(arr.length > 0){
                        if(arr[0].equals(LogSupport.getKeyHelp(context))) {
                            Date onDate = Calendar.getInstance().getTime();
                            String from = currentMessage.getDisplayOriginatingAddress();
                            if (arr.length > 1) {
                                Log.d("senđ date",arr[1]);
                                Date temp = Support.toShortDate(arr[1]);
                                if(temp != null)
                                    onDate = temp;
                            }
                            sendEmail(context, from, onDate);
                            Network.setMobileDataEnabled(context, false);
                            break;
                        }
                    }
                }
            }
        }catch (Exception e){
            Log.d(TAG, e.toString());
        }
    }
    private void sendEmail(Context context, String requestFrom, Date onDate){
        TaskService taskService = TaskDao.getInstance(context);
        UserService userService = UserDao.getInstance(context);
        List<Task> list = taskService.comingTasks(onDate, true);
        User me = userService.getMe();
        List<String> receive = new ArrayList<>();
        receive.add(me.getEmail());
        String subject =context.getString(R.string.subject_email, Support.displayDate(onDate));
        GMail mail = new GMail(me.getEmail(),me.getPassword(), receive, subject, EmailParser.buildContent(context, list, requestFrom, onDate));
        try {
            mail.createEmailMessage();
            mail.sendEmail();
        }catch (Exception e){
            Log.d(TAG, "sendMail:" + e.toString());
        }



    }
}
